from rest_framework.views import APIView
from rest_framework.viewsets import ModelViewSet
from question.serializers.questions_serializers import *
from question.paginators import *
from question.models import *
from rest_framework.generics import ListAPIView,CreateAPIView
from rest_framework.response import Response
from question.paginators import *


class QuestionView3(APIView,MyPage):
    def get(self, request, id):
        if id == '-1':
            queryset = Question.objects.all()
            serializer = QuestionSerializer(queryset, many=True)
            return Response(serializer.data)
        else:
            ss = Label.objects.get(id=id)
            queryone = ss.questions.all()
            serializer = QuestionSerializer(queryone, many=True)
            return Response(serializer.data)


class QuestionView4(APIView,MyPage):


    def get(self, request, id):
        if id == '-1':
            queryset = Question.objects.all()
            serializer = QuestionSerializer(queryset, many=True)
            return Response(serializer.data)
        else:
            ss = Label.objects.get(id=id)
            queryone = ss.questions.all()
            serializer = QuestionSerializer(queryone, many=True)
            return Response(serializer.data)


class QuestionView5(APIView,MyPage):

    def get(self, request, id):
        if id == '-1':
            queryset = Question.objects.all()
            serializer = QuestionSerializer(queryset, many=True)
            return Response(serializer.data)
        else:
            ss = Label.objects.get(id=id)
            queryone = ss.questions.all().filter(reply=0)
            serializer = QuestionSerializer(queryone, many=True)
            return Response(serializer.data)


class QuestionView6(APIView):
    def post(self,request):
        user = request.user
        if user.is_authenticated:
            serializer = QuestionSerializer2(data=request.data)
            serializer.is_valid(raise_exception=True)
            serializer.save(user=request.user)

        return Response({'message':'发布成功',
                         'success':True})







class QuestionView7(APIView):
    def get(self, request, id):
        queryset = Question.objects.get(id=int(id))
        serializer = QuestionSerializer1(queryset)
        return Response(serializer.data)



class QuestionView8(APIView):
    def put(self, request, id):
        queryset = Question.objects.get(id=id)
        count = queryset.useful_count + 1
        queryset.useful_count=count
        queryset.save()
        return Response({'message':'ok',
                        'success':True})

class QuestionView9(APIView):
    def put(self, request, id):
        queryset = Question.objects.get(id=id)
        count = queryset.unuseful_count + 1
        queryset.unuseful_count = count
        queryset.save()
        return Response({'message': 'ok',
                         'success': True})

